package co.yixiang.modules.shop.rest;

import co.yixiang.aop.log.Log;
import co.yixiang.modules.shop.domain.MachineShow;
import co.yixiang.modules.shop.service.MachineShowService;
import co.yixiang.modules.shop.service.dto.MachineShowQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author lijunze
* @date 2020-04-25
*/
@Api(tags = "机展管理")
@RestController
@RequestMapping("/api/machineShow")
public class MachineShowController {

    private final MachineShowService machineShowService;

    public MachineShowController(MachineShowService machineShowService) {
        this.machineShowService = machineShowService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('machineShow:list')")
    public void download(HttpServletResponse response, MachineShowQueryCriteria criteria) throws IOException {
        machineShowService.download(machineShowService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询机展数据")
    @ApiOperation("查询机展数据")
    @PreAuthorize("@el.check('machineShow:list')")
    public ResponseEntity<Object> getMachineShows(MachineShowQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(machineShowService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增机展数据")
    @ApiOperation("新增机展数据")
    @PreAuthorize("@el.check('machineShow:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody MachineShow resources){
        return new ResponseEntity<>(machineShowService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改机展数据")
    @ApiOperation("修改机展数据")
    @PreAuthorize("@el.check('machineShow:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody MachineShow resources){
        machineShowService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除机展数据")
    @ApiOperation("删除机展数据")
    @PreAuthorize("@el.check('machineShow:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        machineShowService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}